////////////////////////////////////////////////////////////////////////////////
//  Copyright 2011 Adam Kidd
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//    http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
////////////////////////////////////////////////////////////////////////////////

package com.arcadefoundry.rest.utils
{
	public class DateFormatter
	{
		//------------------------------------------------
		//
		// Properties
		//
		//------------------------------------------------
		private static const days:Array = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
		private static const months:Array = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
		
		//------------------------------------------------
		//
		// Methods
		//
		//------------------------------------------------
		/** 
		 * Converts a Date object to an RFC822-formatted string (GMT/UTC).
		 */
		public static function toRFC822(date:Date):String
		{
			var output:String = "";
			
			output += days[date.dayUTC] + ", "; 					// E.g. "Tue, "
			output += toTwoDigitString(date.dateUTC) + " "; 		// E.g. "Tue, 15 "
			output += months[date.monthUTC] + " "; 					// E.g. "Tue, 15 Sep "
			output += date.fullYearUTC + " "; 						// E.g. "Tue, 15 Sep 2005 "
			output += toTwoDigitString(date.hoursUTC) + ":"; 		// E.g. "Tue, 15 Sep 2005 08:"
			output += toTwoDigitString(date.minutesUTC) + ":";		// E.g. "Tue, 15 Sep 2005 08:17:"
			output += toTwoDigitString(date.secondsUTC) + " GMT";	// E.g. "Tue, 15 Sep 2005 08:17:43 GMT"
			
			return output;
		}
		
		/**
		 * Converts a number into a two digit string. 
		 * 
		 * @param value The number to convert
		 * @return A two digit string representation of the number e.g. '08' or '27'
		 * 
		 */
		private static function toTwoDigitString(value:Number):String
		{
			var output:String = "";
			if (value < 10)
			{
				output += "0"; // Add leading zero if needed.
			}
			output += value;

			return output;
		}
	}
}
